package es.deusto.ingenieria.tesoroaventurero;

import java.util.ArrayList;
import java.util.List;

import es.deusto.ingenieria.is.formulation.Operator;
import es.deusto.ingenieria.is.formulation.Problem;
import es.deusto.ingenieria.is.formulation.State;
import es.deusto.ingenieria.is.search.Node;
import es.deusto.ingenieria.is.search.SearchMethod;
import es.deusto.ingenieria.tesoroaventurero.Mover.Sentido;

public class TesoroAventureroProblem extends Problem {


	protected void createOperators() {
		super.addOperator(new Mover(Sentido.ABAJO));
		super.addOperator(new Mover(Sentido.ARRIBA));
		super.addOperator(new Mover(Sentido.DERECHA));
		super.addOperator(new Mover(Sentido.IZQUIERDA));

	}

	
	public void solve(SearchMethod searchMethod,  ArrayList<State> estados){
		List<String> operators = new ArrayList<String>();
		List<String> operators2 = new ArrayList<String>();

		for (int i=0; i<estados.size(); i++) {
			this.addFinalState(estados.get(i));
			Node finalNode = searchMethod.search(this, this.getInitialStates().get(0));
			this.getFinalStates().remove(estados.get(i));
			this.getInitialStates().remove(0);
			this.addInitialState(estados.get(i));
			
			if (finalNode != null) {
				System.out.println("Solution found!!");
				searchMethod.solutionPath(finalNode, operators2);
				for (int e=0; e<operators2.size(); e++) {
					operators.add(operators2.get(e));
				}
				searchMethod.createSolutionLog(operators);
				System.out.println(operators2);
				operators2 = new ArrayList<String>();
			} else {
				System.out.println("Unable to find the solution!");
				break;
			}
		}
	}
	
}
